/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DLL;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class ChangePacket {
    public static byte[] ChangePackets(String vflag,int vchecksum,int vsequence,int vacknowledge,String vheader,byte[] vdatas)
    {
        byte[] buf = new byte[1024];
        //Flag
        byte[] flag = new byte[]{1};
        //flag = "1".getBytes();
        flag = vflag.getBytes();
        //Header
        //String iheader ="harayposter.1";
        byte[] header = vheader.getBytes();
        //Header length
        short ddh = (short)header.length; 
        byte[] headerlength = new byte[2];
        headerlength[0] = (byte)(ddh & 0xff);
        headerlength[1] = (byte)((ddh >> 8) & 0xff);
        // Packets length
        byte[] packetlength = new byte[2];
        //short maxlength = 1024; 
        short maxlength = (short)vdatas.length;
        packetlength[0] = (byte)(maxlength & 0xff);
        packetlength[1] = (byte)((maxlength >> 8) & 0xff);
        // check sum
        byte[] checksum = new byte[4];
        //short shortSum = 10240; 
        checksum[0] = (byte) ((vchecksum >> 24) & 0xFF);
        checksum[1] = (byte) ((vchecksum >> 16) & 0xFF);
        checksum[2] = (byte) ((vchecksum >> 8) & 0xFF);
        checksum[3] = (byte) (vchecksum & 0xFF);
        // sequence number
        byte[] sequence = new byte[4];
        //int Sequen = 648;
        sequence[0] = (byte) ((vsequence >> 24) & 0xFF);
        sequence[1] = (byte) ((vsequence >> 16) & 0xFF);
        sequence[2] = (byte) ((vsequence >> 8) & 0xFF);
        sequence[3] = (byte) (vsequence & 0xFF);
        // acknowledge number
        byte[] acknowledge = new byte[4];
        //int acknow = 134245;
        int acknow = vacknowledge;
        acknowledge[0] = (byte) (acknow >> 24);
        acknowledge[1] = (byte) (acknow >> 16);
        acknowledge[2] = (byte) (acknow >> 8);
        acknowledge[3] = (byte) (acknow);
        int lengthdata = 1007 - header.length;
        byte[] datas = new byte[lengthdata];
        //datas = "dtietioehtwuiothiwtji".getBytes();
        datas = vdatas;
          
          int i=0;
          System.arraycopy(flag, 0, buf, 0, flag.length);
          i = flag.length;
          System.arraycopy(headerlength, 0, buf, i, headerlength.length);
          i = i + headerlength.length;
          System.arraycopy(packetlength, 0, buf, i, packetlength.length);
          i = i + packetlength.length;
          System.arraycopy(checksum, 0, buf, i, checksum.length);
          i = i + checksum.length;
          System.arraycopy(sequence, 0, buf, i, sequence.length);
          i = i + sequence.length;
          System.arraycopy(acknowledge, 0, buf, i, acknowledge.length);
          i = i + acknowledge.length;
          System.arraycopy(header, 0, buf, i, header.length);
          i = i + header.length;
          System.arraycopy(datas, 0, buf, i, datas.length);
          //i = i + datas.length;
          return buf;
    }
    
    //Day doc goi tin
      //byte[] reBuf = dgp.getData();
      //byte a = reBuf[0];
      //byte[] index = Arrays.copyOfRange(reBuf, 1, 3);
      //short valueIndex = ByteBuffer.wrap(index).order(ByteOrder.LITTLE_ENDIAN).getShort();
      //byte[] headerlength = Arrays.copyOfRange(reBuf, 3, 5);
      //short valueHeaderlength = ByteBuffer.wrap(headerlength).order(ByteOrder.LITTLE_ENDIAN).getShort();
      //byte[] packets = Arrays.copyOfRange(reBuf, 5, 7);
      //short valuePackets = ByteBuffer.wrap(packets).order(ByteOrder.LITTLE_ENDIAN).getShort();
      //byte[] checksum = Arrays.copyOfRange(reBuf, 7, 9);
      //short valuechecksum = ByteBuffer.wrap(checksum).order(ByteOrder.LITTLE_ENDIAN).getShort();
      //byte[] sequence = Arrays.copyOfRange(reBuf, 9, 13);
      //int valuesequence = ((sequence[0] & 0xFF) << 24) | ((sequence[1] & 0xFF) << 16)
      //  | ((sequence[2] & 0xFF) << 8) | (sequence[3] & 0xFF);
      //byte[] acknowledge = Arrays.copyOfRange(reBuf, 13, 17);
      //int valueacknowledge = ((acknowledge[0] & 0xFF) << 24) | ((acknowledge[1] & 0xFF) << 16)
      //  | ((acknowledge[2] & 0xFF) << 8) | (acknowledge[3] & 0xFF);
      //byte[] header = Arrays.copyOfRange(reBuf, 17, reBuf.length);
}
